#include "key.h"
unsigned char keynum = 255;

sbit row3 = P3^2;
sbit row4 = P3^3;
sbit col1 = P4^4;
sbit col2 = P4^2;

void clear_r(void)
{
	row3 = 0;
	row4 = 0;
	col1 = 1;
	col2 = 1;
}

void clear_c(void)
{
	row3 = 1;
	row4 = 1;
	col1 = 0;
	col2 = 0;
}

void scankeyboard(void)
{
	static unsigned char status = 0;
	static unsigned char midvalue = 0;
	static unsigned char row = 0;
	switch(status)
	{
		case 0:
			clear_c();
			if(row3 == 0 || row4 == 0)
			{
				status = 1;
			}
			break;
		case 1:
			clear_c();
			if(row3 == 0){
				status = 2;
				row = 1;
			}else if(row4 == 0){
				status = 2;
				row = 0;
			}else{
				status = 0;
			}
			break;
		case 2:
			clear_r();
			if(col1 == 0){
				midvalue = 4 + row;
				status = 3;
			}else if(col2 == 0){
				midvalue = 8 + row;
				status = 3;
			}else{
				status = 0;
			}
			break;
		case 3:
			clear_r();
			if(col1 == 1 && col2 == 1)
			{
				keynum = midvalue;
				status = 0;
			}
			break;
	}
}